/**
 * 
 */
var map;
var comments;
var marker;
var geocoder;
var route;
var city;
var region;
var address;
function initialize() {
	geocoder = new google.maps.Geocoder();
	var myOptions = {
		zoom : 18, // 缩放级别
		// 可以使用
		// 0（最低缩放级别，在地图上可以看到整个世界）到
		// 19（最高缩放级别，可以看到独立建筑物）之间的缩放级别
		mapTypeId : google.maps.MapTypeId.ROADMAP, // ROADMAP -
		// 默认视图
		// SATELLITE-显示Google地球卫星图像
		// HYBRID-混合显示普通视图和卫星视图
		// TERRAIN-地形图
		scaleControl : true,// 比例尺
		mapTypeControl : true,
		streetViewControl : true
	};

	map = new google.maps.Map(document.getElementById('maps'), myOptions);
	marker = new google.maps.Marker({
				map : map,
				draggable : true,
				clickable : false
			});
	google.maps.event.addListener(marker, "dragend", function(event) {
				var latLng = event.latLng;
				if (geocoder) {
					geocoder.geocode({
								'latLng' : latLng
							}, function(results, status) {
								if (status == google.maps.GeocoderStatus.OK) {
									if (results[0]) {
										marker.setPosition(latLng);
										marker
												.setTitle(results[0].formatted_address);
									}
								} else {
									alert("导航分析出现问题: " + status);
								}
							});
				}
			});
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(function(position) {
					var pos = new google.maps.LatLng(position.coords.latitude,
							position.coords.longitude);
					marker.setPosition(pos);
					if (position.address != null) {
						comments = "您当前定位的所在国家:" + position.address.country
								+ ",城市:" + position.address.region + ",地区: "
								+ position.address.city + ",所在街道:"
								+ position.address.street
								+ position.address.streetNumber;
					} else {
						comments = "您当前的被移动定位的位置";
					}
					marker.setTitle(comments);
					var infowindow = new google.maps.InfoWindow({
								map : map,
								position : pos,
								content : comments
							});
					map.setCenter(pos);
				}, function() {
					handleNoGeolocation(true);
				});
	} else {
		handleNoGeolocation(false);
	}
}
function searchLocation() {
	var message = document.getElementById('position').value;
	if (geocoder) {
		geocoder.geocode({
					'address' : message
				}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						if (results[0]) {
							var location = results[0].geometry.location;
							marker.setTitle(message);
							map.setCenter(location);
							marker.setPosition(location);
							map.setZoom(16);
						} else {
						}
					} else {
						alert("对不起，你输入的信息不正确，以至于无法导航！ ");
					}
				});
	}
}
function handleNoGeolocation(errorFlag) {
	if (errorFlag) {
		var content = '对不起导航失败！';
	} else {
		var content = '对不起，您使用的浏览器不支持导航系统，请选择谷歌，火狐以及Safari支持Html5的浏览器!';
	}
	var options = {
		map : map,
		content : address
	};
	var infowindow = new google.maps.InfoWindow(options);
	map.setCenter(options.position);
}
function locationMessage(pos) {
	if (geocoder) {
		geocoder.geocode({
					'latLng' : pos
				}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						if (results[1]) {
							address = results[1].formatted_address;
							for (var i = 0; i < eval(results[0].address_components).length; i++) {
								var types = results[0].address_components[i].types;
								for (var j = 0; j < eval(types).length; j++) {
									if (types[0] == "route") {
										route = results[0].address_components[i].long_name;
									} else if (types[0] == "locality") {
										city = results[0].address_components[i].long_name;
									} else if (types[0] == "sublocality") {
										region = results[0].address_components[i].long_name;
									}
								}
							}
							var lat = pos.lat();
							var lng = pos.lng();
							Ext.Ajax.request({
								url : 'registerlocation.action',
								method : 'post',
								params : {
									'lat' : lat,
									'lng' : lng,
									'city' : city,
									'region' : region,
									'route' : route,
									'address' : address
								},
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									if (obj.success == false) {
										Ext.Msg.alert("系统消息", obj.msg);
										return false;
									} else {
										Ext.Msg.alert("系统消息", obj.msg,
												function() {
													document.location.href = "UserIndex";
												});
									}
								}
							});

						} else {
						}
					} else {
						alert("对不起，你输入的信息不正确，以至于无法导航！ ");
					}
				});
	}
}
function register() {
	var pos = marker.getPosition();
	locationMessage(pos);

}